package com.cn.kabalifemerchants.ui.page.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cn.kabalifemerchants.R;
import com.cn.kabalifemerchants.base.IBaseActivity;
import com.cn.kabalifemerchants.config.CustomRefreshListener;
import com.cn.kabalifemerchants.config.LoginHelper;
import com.cn.kabalifemerchants.config.RefreshHelper;
import com.cn.kabalifemerchants.ui.page.adapter.RecruitListMsgAdapter;
import com.cn.kabalifemerchants.ui.page.bean.RecruitDetailsBean;
import com.cn.kabalifemerchants.ui.page.bean.RecruitDetailsMsgListBean;
import com.cn.kabalifemerchants.ui.page.bean.RecruitListBean;
import com.cn.kabalifemerchants.utils.RxToast;
import com.cn.kabalifemerchants.view.NotDataView;
import com.cn.kabalifemerchants.view.PopupWindow.CommonPopupWindow;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.zzhoujay.richtext.ImageHolder;
import com.zzhoujay.richtext.RichText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by 晓林 on 2020/10/20.
 * function:
 */
public class RecruitDetailsActivity extends IBaseActivity<RecruitListView, RecruitListPresenter> implements RecruitListView {

    Bundle bundle;
    int id;
    @BindView(R.id.tv_details)
    TextView tvDetails;
    @BindView(R.id.rv_notice)
    RecyclerView rvNotice;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private View notDataView;
    List<RecruitDetailsMsgListBean.ResultBean.DataBean> mList;
    RecruitListMsgAdapter mAdapter;

    @Override
    protected RecruitListPresenter createPresenter() {
        return new RecruitListPresenter();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.recruit_details;
    }

    @Override
    public void onReLoggedIn(String msg) {
        RxToast.error(msg);
        LoginHelper.againGoToLoginActivity(getMe());
    }

    @Override
    public void onError(String msg) {
        RxToast.error(msg);
    }

    @Override
    public Context _getContext() {
        return null;
    }


    @Override
    public void onRecruitListSuccess(RecruitListBean bean) {

    }

    @Override
    public void onRecruitDetailsSuccess(RecruitDetailsBean bean) {
        if (!TextUtils.isEmpty(bean.getResult().getDuty())) {
//            tvDetails.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
            RichText.fromHtml(bean.getResult().getDuty()).autoFix(false).showBorder(false).autoPlay(true).scaleType(ImageHolder.ScaleType.fit_center) // 图片缩放方式
                    .size(ImageHolder.MATCH_PARENT, ImageHolder.WRAP_CONTENT).into(tvDetails);
        }
    }

    @Override
    public void onRecruitDetailsMsgSuccess(RecruitDetailsMsgListBean bean) {
        RefreshHelper.finishRefresh(refreshLayout, pagehttp);
        if (pagehttp == 1) {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                mAdapter.replaceData(bean.getResult().getData());
            } else {
                mAdapter.replaceData(bean.getResult().getData());
                mAdapter.setEmptyView(notDataView);
            }
        } else {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                mAdapter.addData(bean.getResult().getData());
            }

        }
    }


    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setTitleBar("招聘详情");
        bundle = getIntent().getExtras();
        id = bundle.getInt("id");
        mPresenter.onRecruitDetailsData(LoginHelper.getLonginData().getResult(), id);
        mList = new ArrayList<>();
        LinearLayoutManager layoutManager = new LinearLayoutManager(getMe());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        layoutManager.setSmoothScrollbarEnabled(true);
        layoutManager.setAutoMeasureEnabled(true);
        rvNotice.setLayoutManager(layoutManager);
        rvNotice.setNestedScrollingEnabled(false);
        //创建适配器
        mAdapter = new RecruitListMsgAdapter(R.layout.activity_recruit_item_msg, mList, this);
        //给RecyclerView设置适配器
        rvNotice.setAdapter(mAdapter);
        notDataView = NotDataView.notDataView(this, rvNotice, refreshLayout);
        pagehttp = 1;
        getListData();

    }

    @Override
    public void initRefreshLayout() {
        RefreshHelper.initRefreshLayout(refreshLayout, new CustomRefreshListener() {
            @Override
            public void _onRefresh(RefreshLayout refreshLayout) {
                pagehttp = 1;
                getListData();
            }

            @Override
            public void _onLoadMore(RefreshLayout refreshLayout) {
                pagehttp++;
                if (null != mList) {
                    getListData();
                }
            }
        });
    }

    /**
     * 获取数据。刷新
     */
    public void getListData() {
        mPresenter.onRecruitDetailsMsgData(LoginHelper.getLonginData().getResult(), pagehttp, pageNum, id);
    }

}
